package servlet;

import pojo.Grade;
import pojo.Student;
import service.GradeServiceImpl;
import service.GradeService;
import service.StudentService;
import service.StudentServiceImpl;
import utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {
    StudentService studentService = new StudentServiceImpl();
    GradeService gradeService = new GradeServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("insert".equals(method)){
            addstu(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("delete".equals(method)){
            deletestu(req, resp);
        }else if ("findlist".equals(method)){
            findlist(req, resp);
        }else {
            findbyid(req, resp);
        }
    }
    //删除学员
    protected void deletestu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");
        int i = studentService.deleteById(Integer.parseInt(stuId));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('删除成功');location.href='/studentServlet?method=findlist'</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/studentServlet?method=findlist‘</script>");
        }
    }
    //新增学员
    protected void addstu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String profession = req.getParameter("profession");
        String idNumber = req.getParameter("idNumber");
        String politics = req.getParameter("politics");
        String introduction = req.getParameter("introduction");
        String gid = req.getParameter("gid");
        Student student = new Student();
        student.setStuName(stuName);
        student.setStuNo(stuNo);
        student.setSex(Integer.parseInt(sex));
        student.setPhone(phone);
        student.setEmail(email);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setProfession(profession);
        student.setIdNumber(idNumber);
        student.setPolitics(politics);
        student.setIntroduction(introduction);
        student.setGid(Integer.parseInt(gid));
        student.setRegDate(new Date());
        int i = studentService.addStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功');location.href='/studentServlet?method=findlist'</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGradeList'</script>");
        }
    }
    //主键查询
    protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");
        Student student = studentService.selectById(Integer.parseInt(stuId));
        req.setAttribute("stu",student);
        List<Grade> list = gradeService.selectALl();
        req.setAttribute("gradeList",list);
        req.getRequestDispatcher("/Educational/student/edit.jsp").forward(req,resp);
    }
    //查询列表
    protected void findlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Student> students = null;
        PageUtils pageUtils = new PageUtils();
        //模糊查询
        String stuName = req.getParameter("stuName");
        String stuNo= req.getParameter("stuNo");
        String sex = req.getParameter("sex");
        int sexi = -1;
        if (sex!=null&&sex!=""){
            sexi = Integer.parseInt(sex);
        }
        //分页数据
        //页码值
        String pageIndex = req.getParameter("pageIndex");
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        try {
            students = studentService.selectAll(stuName,stuNo,sexi,index, pageUtils.getPageSize());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //获取总页数
        int totalPage = 0;
        try {

            totalPage = studentService.total(stuName,stuNo,sexi)%5>0?
                    studentService.total(stuName,stuNo,sexi)/5+1:
                    studentService.total(stuName,stuNo,sexi)/5;
            req.setAttribute("total",studentService.total(stuName,stuNo,sexi));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        req.setAttribute("stuList",students);
        req.setAttribute("stuName",stuName);
        req.setAttribute("stuNo",stuNo);
        req.setAttribute("sex",sexi);
        req.setAttribute("pageIndex",index);
        req.setAttribute("totalPage",totalPage);
        req.getRequestDispatcher("/Educational/student/list.jsp").forward(req,resp);
    }
    //修改学员
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuId = req.getParameter("stuId");
        String stuName = req.getParameter("stuName");
        String stuNo = req.getParameter("stuNo");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String profession = req.getParameter("profession");
        String idNumber = req.getParameter("idNumber");
        String politics = req.getParameter("politics");
        String introduction = req.getParameter("introduction");
        String gid = req.getParameter("gid");
        Student student = new Student();
        student.setStuId(Integer.parseInt(stuId));
        student.setStuName(stuName);
        student.setStuNo(stuNo);
        student.setSex(Integer.parseInt(sex));
        student.setPhone(phone);
        student.setEmail(email);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setProfession(profession);
        student.setIdNumber(idNumber);
        student.setPolitics(politics);
        student.setIntroduction(introduction);
        student.setGid(Integer.parseInt(gid));
        student.setRegDate(new Date());
        int i = studentService.updateStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('更新成功');location.href='/studentServlet?method=findlist'</script>");
        }else {
            writer.println("<script>alert('更新失败');location.href='/studentServlet?stuId="+stuId+"</script>");
        }
    }
}
